{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Single Number III"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: singleNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #只出现一次的数字 III"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>nums</code>，其中恰好有两个元素只出现一次，其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 <strong>任意顺序</strong> 返回答案。</p>\n",
    "\n",
    "<p>你必须设计并实现线性时间复杂度的算法且仅使用常量额外空间来解决此问题。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,1,3,2,5]\n",
    "<strong>输出：</strong>[3,5]\n",
    "<strong>解释：</strong>[5, 3] 也是有效的答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,0]\n",
    "<strong>输出：</strong>[-1,0]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,1]\n",
    "<strong>输出：</strong>[1,0]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "\t<li>除两个只出现一次的整数外，<code>nums</code> 中的其他数字都出现两次</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [single-number-iii](https://leetcode.cn/problems/single-number-iii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [single-number-iii](https://leetcode.cn/problems/single-number-iii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,1,3,2,5]', '[-1,0]', '[0,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a = b = 0\n",
    "        for x in nums:\n",
    "            b = (b ^ x) & ~a\n",
    "            a = (a ^ x) & ~b\n",
    "        return b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ones, twos = 0, 0\n",
    "        for num in nums:\n",
    "            ones = ones ^ num & ~twos\n",
    "            twos = twos ^ num & ~ones\n",
    "        return ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total%3:\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans |= (1 << i)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total % 3:\n",
    "                # Python 这里对于最高位需要特殊判断\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans |= (1 << i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total % 3:\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans |= (1 << i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        result = 0\n",
    "        for i in range(32):  # 对于32位整数\n",
    "            count = 0\n",
    "            for num in nums:\n",
    "                count += (num >> i) & 1  # 统计所有数字在第i位上的值的和\n",
    "            if count % 3 != 0:  # 取余，得到只出现一次的那个数字在该数位上的值\n",
    "                result |= 1 << i  # 将该数位上的值加入结果中\n",
    "        return result if result < 2**31 else result - 2**32  # 处理负数的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ones, twos = 0, 0\n",
    "        for num in nums:\n",
    "            # 第一次出现的位\n",
    "            ones = (ones ^ num) & ~twos\n",
    "            # 第二次出现的位\n",
    "            twos = (twos ^ num) & ~ones\n",
    "        return ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            a = sum(((num >> i) & 1) for num in nums)\n",
    "            if a % 3 == 1:\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans += (1 << i)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ones, twos = 0, 0\n",
    "        for num in nums:\n",
    "            ones = ones ^ num & ~twos\n",
    "            twos = twos ^ num & ~ones\n",
    "        return ones\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res,i = 0,0\n",
    "        flag = 0\n",
    "        for x in nums:\n",
    "            if x>0:\n",
    "                flag+=1\n",
    "        flag = flag%3\n",
    "        while i <= 32:\n",
    "            tmp = 1<<i\n",
    "            cnt = 0\n",
    "            for x in nums:\n",
    "                if tmp&abs(x)>0:\n",
    "                    cnt+=1\n",
    "            if cnt%3 > 0:\n",
    "                res+=tmp\n",
    "            i+=1\n",
    "        if flag == 0:\n",
    "            return -1*res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ones, twos = 0, 0\n",
    "        for num in nums:\n",
    "            ones = ones ^ num & ~twos\n",
    "            twos = twos ^ num & ~ones\n",
    "        return ones\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a = b = 0\n",
    "        for num in nums:\n",
    "            b = ~a & (b ^ num)\n",
    "            a = ~b & (a ^ num)\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total % 3:\n",
    "                # Python 这里对于最高位需要特殊判断\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans += (1 << i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            count = 0\n",
    "            for n in nums:\n",
    "                if (n >> i & 1) == 1:\n",
    "                    count += 1\n",
    "            if count % 3 != 0:\n",
    "                res |= 1 << i \n",
    "        return res if count % 3 == 0 else ~(res ^ 0xffffffff)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        d = 0\n",
    "        e = 0\n",
    "        for n in nums:\n",
    "            d, e = (~d & e & n) | (d & ~(e | n)), ~d & (e ^ n)\n",
    "        return e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a = b = 0\n",
    "        for x in nums:\n",
    "            b = (b ^ x) & ~a\n",
    "            a = (a ^ x) & ~b\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        return self.find(nums, 3)\n",
    "\n",
    "    def find(self, arr, m):\n",
    "        cnts = [0] * 32\n",
    "\n",
    "        for num in arr:\n",
    "            for i in range(32):\n",
    "                cnts[i] += (num >> i) & 1\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            if cnts[i] % m != 0:\n",
    "                ans |= 1 << i\n",
    "\n",
    "        # 处理负数的情况\n",
    "        if ans & (1 << 31):\n",
    "            ans -= 1 << 32\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        result = 0\n",
    "        for i in range(32):\n",
    "            s = 0\n",
    "            for num in nums:\n",
    "                s += (num >> i) & 1\n",
    "            s %= 3\n",
    "            if i == 31:\n",
    "                result -= s << i\n",
    "            else:\n",
    "                result += s << i\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        for i in nums:\n",
    "            if nums.count(i) == 1:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "\n",
    "        ## 统计nums中数字各个比特位1出现的个数总和，\n",
    "        counts = [0]*32\n",
    "        for num in nums:\n",
    "            for j in range(32):\n",
    "                counts[j]+= num&1\n",
    "                num>>=1\n",
    "\n",
    "        res = 0\n",
    "        m = 3                       # 模板，可将m改成任意 “出现m次的重复数字，但某个数字只出现一次\n",
    "        for i in range(32):\n",
    "            res<<=1\n",
    "            res |= counts[32-i-1]%m\n",
    "        return res if counts[31] % m == 0 else ~(res ^ 0xffffffff) \n",
    "        \n",
    "        # 按理来说好像只返回 res 即可，但是由于 Python 的存储负数的特殊性（以补码形式存储），需要先将 000 - 32 位取反（即 res ^ 0xffffffff ），再将所有位取反（即 ~ ）。 两个组合操作实质上是将32位以上取反， 0-31 位不变。 实现将负数补码数字还原为Python的存储格式。\n",
    "\n",
    "    ## 思路：出现 m 次的数字，其各个比特位上出现数字 mod(m)之后的结果为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a = b = 0\n",
    "        for x in nums:\n",
    "            b = (b ^ x) & ~a\n",
    "            a = (a ^ x) & ~b\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total % 3:\n",
    "                # Python 这里对于最高位需要特殊判断\n",
    "                # if i == 31:\n",
    "                #     ans -= (1 << i)\n",
    "                # else:\n",
    "                if i == 31:\n",
    "                    print(bin(ans))\n",
    "                    ans -= (1 << i)\n",
    "                    print(bin(ans))\n",
    "                ans |= (1 << i)\n",
    "                # if i == 30:\n",
    "                #     print(bin(ans)[2:])\n",
    "                # if i == 31:\n",
    "                    # ans -= (1 << i)\n",
    "                #     print(bin(ans))\n",
    "                    # print(bin(ans)[2:])\n",
    "                    # print(bin(-4)[2:])\n",
    "                \n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # temp1 = 0\n",
    "        # temp2 = 1\n",
    "        # for num in nums:\n",
    "        #     temp1 += num\n",
    "        #     temp2 ^= num\n",
    "        # if len(nums) == 1:\n",
    "        #     return nums[0]\n",
    "\n",
    "        # total_sum = 0\n",
    "        # single_sum = 0\n",
    "        # carry = 0xffffffff\n",
    "\n",
    "        # for num in nums:\n",
    "        #     single_sum ^= num\n",
    "        #     print('b4', carry)\n",
    "        #     carry = carry & num if carry else 0xffffffff & num\n",
    "        #     print('after', carry)\n",
    "        #     total_sum += num\n",
    "        # # print(carry)\n",
    "        # carry <<= 1\n",
    "        \n",
    "        # print(carry)\n",
    "        # x, y = single_sum, carry\n",
    "        # while y:\n",
    "        #     ans = x ^ y\n",
    "        #     carry = (x & y) << 1\n",
    "        #     x, y = ans, carry\n",
    "        #     # single_sum ^= carry\n",
    "        #     # carry = (single_sum & carry) << 1\n",
    "        # single_sum = x\n",
    "\n",
    "        # print(single_sum)\n",
    "\n",
    "        # return (single_sum*3-total_sum)//2\n",
    "\n",
    "        \n",
    "        \n",
    "\n",
    "        # print(temp1/3, temp2)\n",
    "        # return int((temp2-temp1/3)*(3/2))\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bitSum = sum((x >> i) & 1 for x in nums)\n",
    "            bitSum %= 3\n",
    "            if i == 31:\n",
    "                ans -= bitSum << i\n",
    "            else:\n",
    "                ans |= bitSum << i\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        MASK = 1\n",
    "        memo = [0]*32\n",
    "        for i in range(32):\n",
    "            for j in nums:\n",
    "                if j & MASK == MASK:\n",
    "                    memo[31-i] += 1 \n",
    "            MASK <<=1 \n",
    "\n",
    "        memo = [str(i % 3) for i in memo]\n",
    "        integer = int('0b'+''.join(memo), 2) \n",
    "        if memo[0] == '1':\n",
    "            integer -= 2**32\n",
    "        return integer \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        o,t = 0,0\n",
    "        for n in nums:\n",
    "            o=o^n&~t\n",
    "            t = t^n&~o\n",
    "        return o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        num_arr = [0]*32\n",
    "        for num in nums:\n",
    "            for i in range(32):\n",
    "                if num==0:break\n",
    "                num_arr[i]+=num&1\n",
    "                num>>=1\n",
    "        for i in range(32):\n",
    "            # num_arr[i] %= 3\n",
    "            # if num_arr[i] != 0:\n",
    "            #     ans+=pow(2,i)\n",
    "            ans <<= 1\n",
    "            ans |= num_arr[31-i]%3\n",
    "\n",
    "        if not (num_arr[31]%3):\n",
    "            return ans\n",
    "        else:\n",
    "            return ~(ans^0xffffffff)\n",
    "            \n",
    "            \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        for i in nums:\n",
    "            if nums.count(i)==1:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a = b = 0\n",
    "        for num in nums:\n",
    "            b = ~a & (b ^ num)\n",
    "            a = ~b & (a ^ num)\n",
    "        return b\n",
    "\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        MASK = 1\n",
    "        memo = [0]*32\n",
    "        for i in range(32):\n",
    "            for j in nums:\n",
    "                if j & MASK == MASK:\n",
    "                    memo[31-i] += 1 \n",
    "            MASK <<=1 \n",
    "\n",
    "        memo = [str(i % 3) for i in memo]\n",
    "        integer = int('0b'+''.join(memo), 2) \n",
    "        if memo[0] == '1':\n",
    "            integer -= 2**32\n",
    "        return integer \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ls = [0] *32\n",
    "        for e in nums:\n",
    "            i=0\n",
    "            e= e&0xffffffff\n",
    "            while e:\n",
    "                ls[i]+=e%2\n",
    "                e//=2\n",
    "                i+=1\n",
    "        ls = [e%3 for e in ls]\n",
    "        res = 0\n",
    "        base = 1\n",
    "        for e in ls:\n",
    "            if base==2**31:\n",
    "                res-=e*base\n",
    "                break\n",
    "            res+=e*base\n",
    "            base*=2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(31):\n",
    "            bitSum = sum((x >> i) & 1 for x in nums)\n",
    "            bitSum %= 3\n",
    "            ans |= bitSum << i\n",
    "\n",
    "        if sum((x >> 31) & 1 for x in nums) % 3:\n",
    "            ans -= 1 << 31\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in range(32):\n",
    "            cnt=sum((n>>i)&1 for n in nums)\n",
    "            if cnt%3:\n",
    "                if i==31:\n",
    "                    ans-=1<<i\n",
    "                else:\n",
    "                    ans|=1<<i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num>>i)&1 for num in nums)\n",
    "            if total%3:\n",
    "                if i==31:\n",
    "                    ans-=(1<<i)\n",
    "                else:\n",
    "                    ans+=(1<<i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ones, twos = 0, 0\n",
    "        for num in nums:\n",
    "            ones = ones ^ num & ~twos\n",
    "            twos = twos ^ num & ~ones\n",
    "        return ones\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num >> i) & 1 for num in nums)\n",
    "            if total % 3:\n",
    "                if i == 31:\n",
    "                    ans -= (1 << i)\n",
    "                else:\n",
    "                    ans |= (1 << i)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum((num>>i)&1 for num in nums)\n",
    "            if total%3:\n",
    "                if i==31:\n",
    "                    ans-=(1<<i)\n",
    "                else:\n",
    "                    ans|=(1<<i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        MASK = 1\n",
    "        memo = [0]*32\n",
    "        for i in range(32):\n",
    "            for j in nums:\n",
    "                if j & MASK == MASK:\n",
    "                    memo[31-i] += 1 \n",
    "            MASK <<=1 \n",
    "\n",
    "        memo = [str(i % 3) for i in memo]\n",
    "        integer = int('0b'+''.join(memo), 2) \n",
    "        if memo[0] == '1':\n",
    "            integer -= 2**32\n",
    "        return integer \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        b1, b2 = 0, 0\n",
    "        for n in nums:\n",
    "            b1 = (b1 ^ n) & ~b2\n",
    "            b2 = (b2 ^ n) & ~b1\n",
    "        return b1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            j = 0\n",
    "            while j <len(nums):\n",
    "                if i!=j and nums[i] == nums[j]:\n",
    "                    break\n",
    "                j += 1\n",
    "            if j == len(nums) or nums[i] !=nums[j]:\n",
    "                return nums[i]\n",
    "            i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            total = sum([(num >> i) &1 for num in nums])\n",
    "            if total % 3:\n",
    "                if i == 31:\n",
    "                    ans -= 1 << i\n",
    "                else:\n",
    "                    ans |= 1 << i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in range(31):\n",
    "            cnt = sum([x >> i & 1 for x in nums])\n",
    "            res |= (cnt % 3) << i\n",
    "        if sum([x >> 31 & 1 for x in nums]) % 3:\n",
    "            res -= 1 << 31\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res = 0 \n",
    "        for i in range(32):\n",
    "            total = sum([(num >> i) & 1 for num in nums])\n",
    "            if total % 3 == 1:\n",
    "                if i == 31:\n",
    "                    res -= 1 << i\n",
    "                else:\n",
    "                    j = 1 << i\n",
    "                    res |= j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        # set_nums=set(nums)\n",
    "        # for i in set_nums:\n",
    "        #     if nums.count(i)==1:\n",
    "        #         return i\n",
    "        nums.sort()\n",
    "        dp=[1] * len(nums)\n",
    "        for index in range(1,len(nums)):\n",
    "            if nums[index]==nums[index-1]:\n",
    "                dp[index]=dp[index]+1\n",
    "            elif dp[index-1]==1:\n",
    "                return nums[index-1]\n",
    "        return nums[len(nums)-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        if len(nums) == 1: return nums[0]\n",
    "        pt = 0\n",
    "        while pt < len(nums):\n",
    "            if nums[pt] == nums[pt + 1]:\n",
    "                pt += 3\n",
    "            else:\n",
    "                return nums[pt]\n",
    "            if pt == len(nums) - 1:\n",
    "                return nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        while len(nums) > 3:\n",
    "            if nums[0] == nums[1]:\n",
    "                nums.pop(0)\n",
    "                nums.pop(0)\n",
    "                nums.pop(0)\n",
    "            else:\n",
    "                return nums[0]\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        a=Counter(nums)\n",
    "        for num,key in a.items():\n",
    "            if key==1:\n",
    "                b=num\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            t = sum([(num>>i)&1 for num in nums])\n",
    "            if t % 3:\n",
    "                if i == 31:\n",
    "                    res -= (1 << i)\n",
    "                else:\n",
    "                    res |= (1 << i)\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            total = sum([(num >>i )&1 for num in nums])\n",
    "            if total %3:\n",
    "                if i == 31:\n",
    "                    res -= (1<<i)\n",
    "                else:\n",
    "                    res |= (1<<i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        sort_nums = sorted(nums)\n",
    "        count = 1\n",
    "        pre = sort_nums[0]\n",
    "        for num in sort_nums[1:]:\n",
    "            if pre != num and count == 1:\n",
    "                return pre\n",
    "            elif pre == num and count != 3:\n",
    "                count += 1\n",
    "            elif count == 3 and pre != num:\n",
    "                pre = num\n",
    "                count = 1\n",
    "        return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        if nums[0] != nums[1]:\n",
    "            return nums[0]\n",
    "        if nums[-1] != nums[-2]:\n",
    "            return  nums[-1]\n",
    "        flag = 0\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] == nums[i-1]:\n",
    "                flag = 0\n",
    "            else:\n",
    "                flag += 1\n",
    "            if flag == 2:\n",
    "                return nums[i-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        i = 0\n",
    "        while i < len(nums) - 1:\n",
    "            if nums[i] != nums[i+1]:\n",
    "                return nums[i]\n",
    "            i += 3\n",
    "        return nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        for i in range(0, len(nums)-2, 3):\n",
    "            if nums[i] != nums[i+1]:\n",
    "                return nums[i]\n",
    "        return nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        num_count_map = {}\n",
    "        for num in nums:\n",
    "            num_count_map.setdefault(num, 0)\n",
    "            num_count_map[num] += 1\n",
    "        \n",
    "        for num, count in num_count_map.items():\n",
    "            if count == 1:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        #状态转换机\n",
    "        ones,twos=0,0\n",
    "        for num in nums:\n",
    "            ones=ones^num & ~twos\n",
    "            twos=twos^num & ~ones\n",
    "            print(ones,twos)\n",
    "        return ones\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        num_list = []\n",
    "\n",
    "        for ii in range(len(nums)):\n",
    "            counter=Counter(num_list)\n",
    "            if counter[nums[ii]] ==2:\n",
    "                num_list.remove(nums[ii])\n",
    "                num_list.remove(nums[ii])\n",
    "            else:\n",
    "                num_list.append(nums[ii])\n",
    "\n",
    "        return num_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        d={}\n",
    "        for i in nums:\n",
    "            if i in d:\n",
    "                d[i]+=1\n",
    "            else:\n",
    "                d[i]=1\n",
    "        for i in d.keys():\n",
    "            if d[i]==1:\n",
    "                return i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        for a in dic:\n",
    "            if dic[a] == 1:\n",
    "                return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ans = dict()\n",
    "        for num in nums:\n",
    "            ans[num] = ans.get(num, 0) + 1\n",
    "        for k, v in ans.items():\n",
    "            if v == 1:\n",
    "                return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        # ans = 0\n",
    "        # for i in range(32):\n",
    "        #     x = sum((num>>i)&1 for num in nums)\n",
    "        #     if x % 3:\n",
    "        #         if i == 31:\n",
    "        #             # 负数位或运算\n",
    "        #             ans = ans - (1<<i)\n",
    "        #         else:\n",
    "        #             ans = ans | (1<<i)\n",
    "                    \n",
    "        # return ans\n",
    "\n",
    "        for n, cnt in Counter(nums).items():\n",
    "            if cnt == 1: return n "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        cnt = collections.Counter(nums)\n",
    "        return [num for num, t in cnt.items() if t == 1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#哈希表\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        freq = collections.Counter(nums)\n",
    "        ans = [num for num, occ in freq.items() if occ == 1][0]\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "#位运算\n",
    "# class Solution:\n",
    "#     def singleNumber(self, nums: List[int]) -> int:\n",
    "#         ans = 0\n",
    "#         for i in range(32):\n",
    "#             total = sum((num >> i) & 1 for num in nums)\n",
    "#             if total % 3:\n",
    "#                 # Python 这里对于最高位需要特殊判断\n",
    "#                 if i == 31:\n",
    "#                     ans -= (1 << i)\n",
    "#                 else:\n",
    "#                     ans |= (1 << i)\n",
    "#         return ans\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/single-number-ii/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        for i,j in Counter(nums).items():\n",
    "            if j==1:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        for k, v in Counter(nums).items():\n",
    "            if v == 1:\n",
    "                return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        memo =Counter(nums)\n",
    "        for k,v in memo.items():\n",
    "            if v==1:\n",
    "                return k\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        freq = collections.Counter(nums)\n",
    "        ans = [key for key, value in freq.items() if value == 1][0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        freq = collections.Counter(nums)\n",
    "        return [num for num, occ in freq.items() if occ == 1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        ndict = {}\n",
    "        for n in nums:\n",
    "            if n in ndict:\n",
    "                if ndict[n] == 2:\n",
    "                    del ndict[n]\n",
    "                else:\n",
    "                    ndict[n] += 1\n",
    "            else:\n",
    "                ndict[n] = 1\n",
    "        for _ in ndict.keys():\n",
    "            return _\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        cnt=Counter(nums)\n",
    "        for  x in nums:\n",
    "            if cnt[x]==1:\n",
    "                return x\n",
    "        return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        set1=set(nums)\n",
    "        List1=list(set1)\n",
    "        for j in List1:\n",
    "            if nums.count(j)==1:\n",
    "                return j "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        freq = Counter(nums)\n",
    "        return [num for num, occ in freq.items() if occ == 1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "            else:\n",
    "                dic[i]+=1\n",
    "        return min(dic.items(),key = lambda x:x[1])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        count = Counter(nums)\n",
    "        return min(count,key=count.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        m={}\n",
    "        for num in nums:\n",
    "            if num in m:\n",
    "                m[num]=m[num]+1\n",
    "            else:\n",
    "                m[num]=1\n",
    "        for k,v in m.items():\n",
    "            if v==1:\n",
    "                return k        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        x = {}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in x:\n",
    "                x[nums[i]] = 1\n",
    "            else:\n",
    "                x[nums[i]] = x[nums[i]] + 1\n",
    "        for i in range(len(nums)):\n",
    "            if x[nums[i]] == 1:\n",
    "                return nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        freq = collections.Counter(nums)\n",
    "        ans = [num for num,occ in freq.items() if occ == 1][0]\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        d1 = sum(set(nums))\n",
    "        return (3*d1 - sum(nums))//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        return (sum(set(nums)) * 3 - sum(nums)) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        hash={}\n",
    "        for i in nums:\n",
    "            if i not in hash:\n",
    "                hash[i]=1\n",
    "            else:\n",
    "                hash[i]+=1\n",
    "        for i in hash.keys():\n",
    "            if hash[i]==1:\n",
    "                return i\n",
    "                break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        occ_dict = collections.Counter(nums)\n",
    "        for k, v in occ_dict.items():\n",
    "            if v == 1:\n",
    "                return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> int:\n",
    "        nums_set = set(nums)\n",
    "        len_s = len(nums)\n",
    "\n",
    "        if len_s == 1:\n",
    "            ress = nums[0]\n",
    "        else:\n",
    "            res = len_s\n",
    "            for i in nums_set:\n",
    "                res1 = nums.count(i)\n",
    "                if res1 < res:\n",
    "                    res = min(res, res1)\n",
    "                    ress = i\n",
    "        return ress"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        count: Dict[int, int] = Counter(nums)\n",
    "        return [n for n, c in count.items() if c == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        n = 0\n",
    "        for num in nums:\n",
    "            n ^= num\n",
    "        n &= -n\n",
    "        result = [0, 0]\n",
    "        for num in nums:\n",
    "            if n & num:\n",
    "                result[0] ^= num\n",
    "            else:\n",
    "                result[1] ^= num\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        xor, ans = 0, [0, 0]\n",
    "        for num in nums:\n",
    "            xor ^= num\n",
    "        xor &= -xor\n",
    "        for num in nums:\n",
    "            if num & xor:\n",
    "                ans[0] ^= num\n",
    "            else:\n",
    "                ans[1] ^= num\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import reduce\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        res=reduce(operator.xor,nums)\n",
    "        for i in nums:\n",
    "            if i^res in nums:\n",
    "                return [i,i^res]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        s = set()\n",
    "        for n in nums:\n",
    "            if n in s:\n",
    "                s.remove(n)\n",
    "            else:\n",
    "                s.add(n)\n",
    "        return list(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        tar_nums = list(set(nums.copy()))\n",
    "        show_nums = []\n",
    "        for item in nums:\n",
    "            if item in show_nums:\n",
    "                tar_nums.remove(item)\n",
    "            show_nums.append(item)\n",
    "        return tar_nums\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        s1=set()\n",
    "        s2=set()\n",
    "        for x in nums:\n",
    "            if x in s1:\n",
    "                s2.add(x)\n",
    "            s1.add(x)\n",
    "        return list(s1-s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        empty_li=[]\n",
    "        li2=[]\n",
    "        for i in nums:\n",
    "            if i not in empty_li:\n",
    "                empty_li.append(i)\n",
    "            else:\n",
    "                li2.append(i)\n",
    "        return list(set(empty_li)-set(li2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        num_dict = {}\n",
    "        for num in nums:\n",
    "            key = str(num)\n",
    "            if key in num_dict:\n",
    "                num_dict[key] += 1\n",
    "            else:\n",
    "                num_dict[key] = 1\n",
    "        result_list = []\n",
    "        for i in num_dict:\n",
    "            if num_dict[i] == 1:\n",
    "                result_list.append(int(i))\n",
    "        return result_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        from collections import Counter\n",
    "        z = Counter(nums)\n",
    "        print(z)\n",
    "        res =[]\n",
    "        for k,v in z.items():\n",
    "            if v==1:\n",
    "                res.append(k)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        stack = []\n",
    "        \n",
    "        for num in nums:\n",
    "            if num in stack:\n",
    "                stack.remove(num)\n",
    "            else:\n",
    "                stack.append(num)\n",
    "                \n",
    "        return stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        from functools import reduce \n",
    "        xor = reduce(lambda x,y:x^y,nums)\n",
    "        ii = xor^(xor & (xor -1))\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for n in nums:\n",
    "            if n & ii == 0:\n",
    "                a = a ^ n\n",
    "            else:\n",
    "                b = b ^ n\n",
    "        return [a,b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        temp=Counter(nums)\n",
    "        return [i for i in temp if temp[i]==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        mask = 0\n",
    "        for i in nums:\n",
    "            mask ^= i\n",
    "        diff = mask & (-mask)\n",
    "        x = 0\n",
    "        for i in nums:\n",
    "            if i & diff:\n",
    "                x ^= i\n",
    "        return [x, mask^x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        diff = reduce(lambda x, y: x ^ y, nums)\n",
    "        lastChar = diff ^ (diff & (diff - 1))\n",
    "        num1 = num2 = 0\n",
    "        for i in nums:\n",
    "            if i & lastChar:\n",
    "                num1 ^= i\n",
    "            else:\n",
    "                num2 ^= i\n",
    "        return [num1, num2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            res ^= i\n",
    "        # 找结果中的1的位置，就可以吧数组分为两组\n",
    "        # 结果数一定在这两个数组中\n",
    "        x, y = 0, 0\n",
    "        h = res & (-res)\n",
    "        for i in nums:\n",
    "            if i & h:\n",
    "                x ^= i\n",
    "            else:\n",
    "                y ^= i\n",
    "        return [x, y]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        sum = 0\n",
    "        for num in nums:\n",
    "            sum ^= num\n",
    "        a = 0\n",
    "        b = 0\n",
    "        index = 0\n",
    "        while not (sum & 1):\n",
    "            index += 1\n",
    "            sum = sum >> 1\n",
    "        for num in nums:\n",
    "            if num >> index & 1:\n",
    "                a ^= num\n",
    "            else:\n",
    "                b ^= num\n",
    "        return a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        counter = Counter(nums)\n",
    "        return list(filter(lambda i: counter[i] == 1, counter))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        res=[]\n",
    "        nums.sort()\n",
    "        nums2=set(nums)\n",
    "        for num in nums2:\n",
    "                nums.remove(num)\n",
    "        for j in nums2:\n",
    "            if j not in nums:\n",
    "                res.append(j)\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        nums1 = list(set(nums))\n",
    "        from collections import Counter\n",
    "        cn,cn1 = Counter(nums),Counter(nums1)\n",
    "        cn2 = cn - cn1\n",
    "        return list((cn1-cn2).elements())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        a = 0\n",
    "        for num in nums:\n",
    "            a ^= num\n",
    "        \n",
    "        part = a & (-a)\n",
    "\n",
    "        m = 0\n",
    "        n = 0\n",
    "\n",
    "        for num in nums:\n",
    "            if num & part:\n",
    "                m ^= num\n",
    "            else:\n",
    "                n ^= num\n",
    "                \n",
    "        return [m, n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        xor_sum = 0\n",
    "        for num in nums:\n",
    "            xor_sum ^= num\n",
    "        k = xor_sum & -xor_sum\n",
    "        type1, type2 = 0, 0\n",
    "        for num in nums:\n",
    "            if k & num == 0:\n",
    "                type1 ^= num\n",
    "            else:\n",
    "                type2 ^= num\n",
    "        return [type1, type2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        # 首先把所有数据异或起来\n",
    "        ans = 0 \n",
    "        for c in nums:\n",
    "            ans ^= c\n",
    "        n1 = 0\n",
    "        n2 = 0\n",
    "        for i in range(32):\n",
    "            x = (ans>>i) & 1\n",
    "            if x==1:\n",
    "                for c in nums:\n",
    "                    if ((c>>i)&1)==1: \n",
    "                        n1^=c\n",
    "                        print(bin(c))\n",
    "                    else: \n",
    "                        n2^=c\n",
    "                break\n",
    "        return [n1,n2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        x=0\n",
    "        for i in nums:\n",
    "            x=x^i\n",
    "        right1=x&(~x+1)\n",
    "        a=0\n",
    "        b=0\n",
    "        for i in nums:\n",
    "            if i&right1:\n",
    "                a^=i\n",
    "            else:\n",
    "                b^=i\n",
    "        return [a,b]\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        # 所有出现两次的相同元素，相互异或为0，那么全部异或得到最终答案的两个数的异或结果\n",
    "        # 这个异或结果可以告诉我们他们俩哪些位是不同的\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            ans ^= num\n",
    "        ans &= -ans\n",
    "        ans1 = ans2 = 0\n",
    "        for num in nums:\n",
    "            # 根据这位1将所有num分成两组，两个不同的数会在两组，其他仍然是相同的\n",
    "            if num & ans:\n",
    "                ans1 ^= num\n",
    "            else:\n",
    "                ans2 ^= num\n",
    "        return [ans1, ans2]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        x = 0\n",
    "        x1 = 0\n",
    "        x2 = 0\n",
    "        for each in nums:\n",
    "            x = x^each\n",
    "        temp = x & -x\n",
    "        for each in nums:\n",
    "            if each&temp == 0 :\n",
    "                x2 = x2^each\n",
    "            else:\n",
    "                x1 = x1^each\n",
    "        return[x1,x2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        s=Counter(nums)\n",
    "        res=[]\n",
    "        for i in s:\n",
    "            if s[i]==1:\n",
    "                res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        a = b = 0\n",
    "        ret = 0\n",
    "        for n in nums:\n",
    "            ret ^= n\n",
    "\n",
    "        h = 1\n",
    "        while ret & h == 0:\n",
    "            h <<= 1\n",
    "        for n in nums:\n",
    "            if h & n == 0:\n",
    "                a ^= n\n",
    "            else:\n",
    "                b ^= n\n",
    "        return [a, b]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        res=0\n",
    "        for num in nums:\n",
    "            res^=num\n",
    "        lsb=res&(-res)\n",
    "        a=b=0\n",
    "        for num in nums:\n",
    "            if num & lsb:\n",
    "                a^=num\n",
    "            else:\n",
    "                b^=num\n",
    "        return [a,b] if a<b else [b,a]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        xorSum = reduce(xor, nums)\n",
    "        lowbit = xorSum & -xorSum\n",
    "        ans = [0,0]\n",
    "        for x in nums:\n",
    "            ans[x&lowbit==0] ^= x\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        xsum = 0\n",
    "        for num in nums:\n",
    "            xsum = xsum ^ num\n",
    "\n",
    "        lowbit = xsum & (-xsum)\n",
    "        type1 = type2 = 0\n",
    "        for num in nums:\n",
    "            if num & lowbit:\n",
    "                type1 = type1 ^ num\n",
    "            else:\n",
    "                type2 = type2 ^ num\n",
    "\n",
    "        return [type1, type2] \n",
    "        # xorsum = 0\n",
    "        # for num in nums:\n",
    "        #     xorsum = xorsum ^ num\n",
    "        \n",
    "        # lsb = xorsum & (-xorsum)\n",
    "        # type1 = type2 = 0\n",
    "        # for num in nums:\n",
    "        #     if num & lsb:\n",
    "        #         type1 = type1 ^ num\n",
    "        #     else:\n",
    "        #         type2 = type2 ^ num\n",
    "        \n",
    "        # return [type1, type2]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def singleNumber(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        return [x for x,y in zip(cnt.keys(),cnt.values()) if y == 1]\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
